/*
 * PCTimersFrmIO.java
 *
 * Created on 9 de octubre de 2006, 08:34 AM
 *
 
 */

package inc.jmc.cyberboard.persistence;

import java.util.Iterator;
import java.util.List;
import inc.jmc.cyberboard.log.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.Marshaller;
import inc.jmc.cyberboard.ui.*;
import inc.jmc.cyberboard.dao.*;
import java.io.*;
import inc.jmc.cyberboard.ui.*;
import inc.jmc.cyberboard.*;
import inc.jmc.cyberboard.persistence.xsd.pctimers.impl.*;
import inc.jmc.cyberboard.common.*;
import org.apache.log4j.Logger;

/**
 *
 * @author Juan Miguel Olguin Salguero
 */
public class PCTimersFrmIO implements Streamable{
    
    static Logger logger = Logger.getLogger(PCTimersFrmIO.class.getName());
    
    /** Creates a new instance of PCTimersFrmIO */
    public PCTimersFrmIO() {
        
    }
    public java.util.ArrayList<PCTimers> load(InternalClock clock)throws Exception{
        java.util.ArrayList<PCTimers> arryRet=null;
        try{
            arryRet=new java.util.ArrayList<PCTimers>();
            java.sql.Connection conn=Dao.getConnection();
            SerialObjectsDao ser=new SerialObjectsDao();
            String xml=ser.query(conn,PCTimerListImpl.class.getName());
            conn.close();
        
            JAXBContext jc = JAXBContext.newInstance("inc.jmc.cyberboard.persistence.xsd.pctimers");
            Unmarshaller u = jc.createUnmarshaller();
            //System.out.println(u.unmarshal(new ByteArrayInputStream(xml.getBytes())).getClass());
            PCTimerListImpl
                    listaImpl= (PCTimerListImpl) u.unmarshal(new ByteArrayInputStream(xml.getBytes()));
            List pctimersList=listaImpl.getPcTimer();
            Iterator it=pctimersList.iterator();
            while(it.hasNext())
            {
                PCTimerImpl pctimerItem=(PCTimerImpl)it.next();
                PCTimers timerGui=new PCTimers(clock);
                timerGui.getCobrarTxt().setText(pctimerItem.getCobrar());
                timerGui.getDisplayTimerTxt().setText(pctimerItem.getTiempo());
                timerGui.getNombreCompuTxt().setText(pctimerItem.getNombre());
                timerGui.getTituloFrontalLabel().setText(pctimerItem.getNombre());
                timerGui.getCronometroLabel().setToolTipText(pctimerItem.getNombre());
                timerGui.getIpTxt().setText(pctimerItem.getIp());
                timerGui.getCobrarTxt().setText(pctimerItem.getCobrar());
                //timerGui.getJTabbedPane1().setSelectedIndex(pctimerItem.getTabSelected());
                timerGui.getJTabbedPane1().setSelectedIndex(pctimerItem.getTabSelected());
                timerGui.setLocation(pctimerItem.getX(),pctimerItem.getY());
                timerGui.setSize(pctimerItem.getTamx(),pctimerItem.getTamy());
                timerGui.setTitle(pctimerItem.getTitle());
                ///nuevas cosas
                timerGui.getInicioTxt().setText(pctimerItem.getHoraInicio());
                timerGui.getFinTxt().setText(pctimerItem.getHoraFin());
                timerGui.getPcLabel().setIcon(ImageLoader.images[pctimerItem.getIconId()]);
                arryRet.add(timerGui);
	     }
	}
	catch(JAXBException jaxbe)
	{
            jaxbe.printStackTrace();
	}
        return arryRet;
    }
   
    public String save(java.util.ArrayList pcTimersArry)throws Exception
    {
      
        String ret=null;
	//try{

            JAXBContext jc = JAXBContext.newInstance("inc.jmc.cyberboard.persistence.xsd.pctimers");
            Marshaller m = jc.createMarshaller();
            m.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT,
                          Boolean.TRUE);
            
            PCTimerListImpl secuencia= new PCTimerListImpl();
            List listPCTimers=secuencia.getPcTimer();
            
            Iterator it=pcTimersArry.iterator();
            while(it.hasNext())
            {
                inc.jmc.cyberboard.ui.PCTimers aux=(inc.jmc.cyberboard.ui.PCTimers)it.next();
                String cobrar=aux.getCobrarTxt().getText();
                String nombre=aux.getNombreCompuTxt().getText();
                String horaTranscurrida=aux.getDisplayTimerTxt().getText();

                PCTimerImpl pctimerimpl=new PCTimerImpl();
                pctimerimpl.setCobrar(cobrar);
                pctimerimpl.setNombre(nombre);
                pctimerimpl.setIp(aux.getIpTxt().getText());
                pctimerimpl.setTiempo(horaTranscurrida);
                pctimerimpl.setX(aux.getX());
                pctimerimpl.setY(aux.getY());
                pctimerimpl.setTamx(aux.getWidth());
                pctimerimpl.setTamy(aux.getHeight());
                pctimerimpl.setTabSelected(aux.getJTabbedPane1().getSelectedIndex());
                pctimerimpl.setTitle(aux.getTitle());
                ///nuevas cosas
                pctimerimpl.setMaquina(nombre);
                pctimerimpl.setHoraInicio(aux.getInicioTxt().getText());
                pctimerimpl.setHoraFin(aux.getFinTxt().getText());
                ///
                pctimerimpl.setIconId(ImageLoader.getIdIcon((javax.swing.ImageIcon)aux.getPcLabel().getIcon() ));
                listPCTimers.add(pctimerimpl);
            }
            java.io.StringWriter swr= new java.io.StringWriter();
            m.marshal( secuencia, swr );
            ret=swr.toString();
            logger.info("ya serializamos a todos los pctimers ["+ret.length()+"]\n"+ret);
            java.sql.Connection conn=Dao.getConnection();
            SerialObjectsDao ser=new SerialObjectsDao();
            logger.info("llave de insercion \n"+secuencia.getClass().getName());
            ser.update(conn,PCTimerListImpl.class.getName(),ret);
            conn.close();
         /* }
          catch(JAXBException ex)
          {
          ex.printStackTrace();
          }
          catch(ClassNotFoundException ex)
          {
          ex.printStackTrace();
          }
          catch(java.sql.SQLException ex)
          {
          ex.printStackTrace();
          }*/
       
        return ret;
        
    }
}


 /*
  *este codigo se 
        tarifasList=Tarifa.getTarifas();
        FileOutputStream fos = new FileOutputStream("D:\\jmdevelopment\\CyberCaffeeNet\\tarifasfrm.tmp");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(tarifasList);
        oos.close();
        
        tarifasList=Tarifa.getTarifas();
        FileOutputStream fos = new FileOutputStream("D:\\jmdevelopment\\CyberCaffeeNet\\tarifasfrm.tmp");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(tarifasList);
        oos.close();
         */